package Ep04_String.Offer_05;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/18 16:54
 * 说明：力扣剑指 offer_05替换空格 ----> 滑动窗口正经解法
 */

public class SlidingWindow {
    public String replaceSpace(String s) {
        if (s == null || s.length() == 0){
            return s;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' '){
                sb.append("  ");
            }
        }
        if (sb.length() == 0){
            return s;
        }
        int left = s.length() - 1;
        s += sb.toString();
        int right = s.length() - 1;
        char[] chars = s.toCharArray();
        while(left >= 0){
            if (chars[left] == ' '){
                chars[right--] = '0';
                chars[right--] = '2';
                chars[right] = '%';
            } else {
                chars[right] = chars[left];
            }
            left--;
            right--;
        }
        return new String(chars);
    }
}
